<?php include("includes/connect_db.php"); ?>
<?php include("includes/core.php"); ?>
<?php
	$idresto = $_GET['idresto'];
	$bill = $_GET['bill'];

	function putTabs($charLength, $tabs){
		$div = $charLength%8;
		$mod = floor($charLength/8);
		$tabChar = "";
		for ($i=0; $i < ($tabs - $mod); $i++) { 
			$tabChar .= "\t";
		}
		return $tabChar;
	}

	function putSpace($charLength, $space){
		$spaceChar = "";
		for ($i=0; $i < ($space - $charLength) ; $i++) { 
			$spaceChar .= " ";
		}
		return $spaceChar;
	}

	function splitChar($word){
		$menu = str_split($word,18);
		return $menu;
	}

	// SELECTING TAX SERVICE
	$sql_select_tax_service = "SELECT * FROM setting";
	$query_select_tax_service = mysql_query($sql_select_tax_service);
	$data_tax_service = mysql_fetch_array($query_select_tax_service);
	$tax_service = $data_tax_service["tax"]+$data_tax_service["service"];

	$format = "Invoice\t\t\t\t\t   ".date("Y-m-d h:i:s")."\r\n";
	$format .= "Puri Nusa Villa | Lembongan Island - Bali\r\n";
	$format .= "purinusa@hotmail.com / +62 366 559 63 72\r\n\r\n";
	$format .= "------------------------------------------\r\n";
	$format .= "\tTitle\t      Price  Qty   Amount\r\n";
	$format .= "------------------------------------------\r\n";

	$grand_total = 0;
	$sql_resto_menu = "SELECT * FROM resto_menu WHERE idresto = '$idresto' AND bill = '$bill'";
	$query_resto_menu = mysql_query($sql_resto_menu);
	while ($resto_menu = mysql_fetch_array($query_resto_menu)) {
		$total = $resto_menu['harga'] * $resto_menu['jumlah'];

		$sql_menu = "SELECT * FROM menu WHERE id = '$resto_menu[idmenu]'";
		$query_menu = mysql_query($sql_menu);
		$menu = mysql_fetch_array($query_menu);
		$menuSplit = splitChar($menu['nama']);
		$iMenu = 1;
		foreach ($menuSplit as $key => $value) {
			# code...
			$format .= $value;
			if ($iMenu > 1) {
				$format .= "\r\n";
			} else {
				$format .= putSpace(strlen($value), 18);
				$format .= putSpace(strlen(number_format($resto_menu['harga'], 0 ,'.', ',')), 10).number_format($resto_menu['harga'], 0 ,'.', ',')." ";
				$format .= putSpace(strlen(number_format($resto_menu['jumlah'], 0 ,'.', ',')), 3).number_format($resto_menu['jumlah'], 0 ,'.', ',')."  ";
				$format .= putSpace(strlen(number_format($total, 0 ,'.', ',')), 8).number_format($total, 0 ,'.', ',')."\r\n";
			}
			$iMenu++;
		}

		$grand_total = $grand_total + $total;
	} 

	$format .= "------------------------------------------\r\n\r\n";
	$tax_service_char = $tax_service."%";
	$grande = $grand_total+($grand_total*$tax_service/100);
	$format .= "\t\t\tTotal:  ".putSpace(strlen(number_format($grand_total, 0 ,'.', ',')), 10).number_format($grand_total, 0 ,'.', ',')."\r\n";
	$format .= "\t\tTax & Service:        ".putSpace(strlen($tax_service_char), 4).$tax_service_char."\r\n";
	$format .= "\t\t  Grand Total: ".putSpace(strlen(number_format($grande, 0 ,'.', ',')), 11).number_format($grande, 0 ,'.', ',')."\r\n\r\n";
	if (isset($_GET["payment_id"])) {
		switch ($_GET["payment_id"]) {
			//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
			case '1':
			$format .= "\t      Payment by Cash: ".putSpace(strlen(number_format($_GET["payment_cash"], 0 ,'.', ',')), 11).number_format($_GET["payment_cash"], 0 ,'.', ',')."\r\n";
			$format .= "\t\t  Your Change: ".putSpace(strlen(number_format(($_GET["payment_cash"] - ($grand_total+($grand_total*$tax_service/100))), 0 ,'.', ',')), 11).number_format(($_GET["payment_cash"] - ($grand_total+($grand_total*$tax_service/100))), 0 ,'.', ',')."\r\n\r\n\r\n";
			break;
			//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
			case '2':
			$credit_tax_char = $_GET["credit_tax"]."%";
			$format .= "\t\t  Card Charge:        ".putSpace(strlen($credit_tax_char), 4).$credit_tax_char."\r\n";
			$format .= "\t\t Full Payment: ".putSpace(strlen(number_format(($grand_total+($grand_total*$tax_service/100)) + (($grand_total+($grand_total*$tax_service/100)) * $_GET["credit_tax"] / 100), 0 ,'.', ',')), 11).number_format(($grand_total+($grand_total*$tax_service/100)) + (($grand_total+($grand_total*$tax_service/100)) * $_GET["credit_tax"] / 100), 0 ,'.', ',')."\r\n\r\n\r\n";
			break;
			//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
			case '3':
			$format .= "\t   Payment by Voucher: ".putSpace(strlen(number_format($_GET["payment_cash"], 0 ,'.', ',')), 11).number_format($_GET["payment_cash"], 0 ,'.', ',')."\r\n\r\n\r\n";
			break;
		}
	}

	$format .= "------------------------------------------\r\n";
	$format .= "Your Dreamy Tropical Paradise Has\r\nOfficially Turned To Reality\r\n\r\n\r\n\r\n\r\n\r\n";
	echo $format;
?>